# 构建阶段
FROM golang:1.19-alpine AS builder

# 设置工作目录
WORKDIR /app

# 安装基础工具
RUN apk add --no-cache git gcc musl-dev

# 复制依赖文件
COPY go.mod go.sum ./

# 下载依赖
RUN go mod download

# 复制源代码
COPY . .

# 编译
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o server cmd/main.go

# 运行阶段
FROM alpine:latest

# 安装基础工具
RUN apk --no-cache add ca-certificates tzdata

# 设置时区
ENV TZ=Asia/Shanghai

# 创建非root用户
RUN adduser -D -g '' appuser

# 创建必要目录
RUN mkdir -p /app/logs && \
    mkdir -p /app/config && \
    chown -R appuser:appuser /app

# 切换到非root用户
USER appuser

# 设置工作目录
WORKDIR /app

# 从构建阶段复制文件
COPY --from=builder /app/server .
COPY --from=builder /app/config ./config
COPY --from=builder /app/static ./static

# 暴露端口
EXPOSE 8080

# 启动命令
CMD ["./server", "-config", "config/config.prod.yaml"]